//
//  VRFirefightingHeaderView.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/9.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRFirefightingHeaderView.h"


@interface VRFirefightingHeaderView()

@property (nonatomic, strong) UILabel *timeLbl;
@property (nonatomic, strong) UILabel *statusLbl;
@property (nonatomic, strong) UILabel *statusTitleLbl;
@property (nonatomic, strong) UIImageView *statusImgView;

@end

@implementation VRFirefightingHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    
    self.backgroundColor = [UIColor whiteColor];
    
    UILabel *timeLbl = [[UILabel alloc] init];
//    timeLbl.text = @"2018年08月09日 23:18:08";
    timeLbl.textColor = RGB(51, 51, 51);
    timeLbl.font = VRFont(16);
    [self addSubview:timeLbl];
    self.timeLbl = timeLbl;
    [timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(30);
        make.height.mas_equalTo(16);
    }];
    
    UILabel *statusTitleLbl1 = [[UILabel alloc] init];
    statusTitleLbl1.text = @"当前状态：";
    statusTitleLbl1.textColor = RGB(51, 51, 51);
    statusTitleLbl1.font = VRFont(16);
    [self addSubview:statusTitleLbl1];
    [statusTitleLbl1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(timeLbl.mas_bottom).offset(6);
    }];
    
    
    UILabel *statusLbl = [[UILabel alloc] init];
//    statusLbl.text = @"异常";
//    statusLbl.textColor = RGB(53, 53, 53);
    statusLbl.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:statusLbl];
    self.statusLbl = statusLbl;
    [statusLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(statusTitleLbl1.mas_right).offset(0);
        make.centerY.mas_equalTo(statusTitleLbl1);
    }];
    
    XButton *viewMonitorBtn = [XButton buttonWithType:UIButtonTypeCustom];
//    goCardPackageBtn.layer.cornerRadius = 2;
    //    goCardPackageBtn.layer.borderColor = RGB(248, 177, 90).CGColor;
    viewMonitorBtn.layer.borderColor = MAINColor.CGColor;
    viewMonitorBtn.layer.borderWidth = 1.0;
    [viewMonitorBtn setTitle:@"查看监控" forState:UIControlStateNormal];
    viewMonitorBtn.titleLabel.font = VRFont(14);
    [viewMonitorBtn setTitleColor:MAINColor forState:UIControlStateNormal];
    [viewMonitorBtn setTitleColor:RGB(211, 209, 209) forState:UIControlStateDisabled];
    [viewMonitorBtn addTarget:self action:@selector(onViewMonitorBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:viewMonitorBtn];
    [viewMonitorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(30);
        make.width.mas_equalTo(76);
        make.height.mas_equalTo(32);
    }];
    
    UIImageView *statusImgView = [[UIImageView alloc] init];
    statusImgView.userInteractionEnabled = YES;
    statusImgView.image = [UIImage imageNamed:@"i_error"];
    statusImgView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:statusImgView];
    self.statusImgView = statusImgView;
    [statusImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(statusLbl.mas_bottom).offset(8);
        make.width.mas_equalTo(116);
        make.height.mas_equalTo(116);
    }];
    
    UILabel *statusTitleLbl = [[UILabel alloc] init];
    statusTitleLbl.text = @"状态异常！";
    statusTitleLbl.textColor = RGB(237, 12, 25);
    statusTitleLbl.font = VRFont(17);
    [self addSubview:statusTitleLbl];
    self.statusTitleLbl = statusTitleLbl;
    [statusTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(statusImgView.mas_bottom).offset(10);
    }];
    
    
    UITapGestureRecognizer *tapGeture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [statusImgView addGestureRecognizer:tapGeture];
}


#pragma mark - action
- (void)onViewMonitorBtnClick:(UIButton *)sender
{
    if (self.viewMonitorBlock) {
        self.viewMonitorBlock();
    }
}

- (void)tapGesture:(UITapGestureRecognizer *)tapGesture
{
    if (self.statusClickBlock) {
        self.statusClickBlock();
    }
}

#pragma setter && getter
- (void)setIsNormal:(BOOL)isNormal
{
    
    
    if (isNormal) {
        self.statusLbl.text = @"安全";
        self.statusLbl.textColor = RGB(78, 172, 57);
        self.statusTitleLbl.text = @"消防安全倾力守护中";
        self.statusTitleLbl.textColor = RGB(78, 172, 57);
        self.statusImgView.image = [UIImage imageNamed:@"i_safe"];
    }else{
        self.statusLbl.text = @"异常";
        self.statusLbl.textColor = RGB(225, 11, 24);
        self.statusTitleLbl.text = @"状态异常！";
        self.statusTitleLbl.textColor = RGB(225, 11, 24);
        self.statusImgView.image = [UIImage imageNamed:@"i_error"];
    }
}

- (void)setStatus:(NSInteger)status
{
    _status = status;
    
    if (status == 0) {
        self.statusLbl.text = @"安全";
        self.statusLbl.textColor = RGB(78, 172, 57);
        self.statusTitleLbl.text = @"消防安全倾力守护中";
        self.statusTitleLbl.textColor = RGB(78, 172, 57);
        self.statusImgView.image = [UIImage imageNamed:@"i_safe"];
    }else if(status == 3){
        
        self.statusLbl.text = @"离线";
        self.statusLbl.textColor = RGB(225, 11, 24);
        self.statusTitleLbl.text = @"设备离线！";
        self.statusTitleLbl.textColor = RGB(225, 11, 24);
        self.statusImgView.image = [UIImage imageNamed:@"i_off_line"];
    }else{
        
        self.statusLbl.text = @"异常";
        self.statusLbl.textColor = RGB(225, 11, 24);
        self.statusTitleLbl.text = @"状态异常！";
        self.statusTitleLbl.textColor = RGB(225, 11, 24);
        self.statusImgView.image = [UIImage imageNamed:@"i_error"];
    }
}


- (void)refreshTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";
    self.timeLbl.text = [formatter stringFromDate:[NSDate date]];
}


@end
